package com.example.manager.config;

import com.example.manager.handler.LoginAuthInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * 解决跨域访问问题
 */
@Component
public class WebMvcConfiguration implements WebMvcConfigurer {

    @Autowired
    private LoginAuthInterceptor loginAuthInterceptor;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(loginAuthInterceptor)
                //接口白名单:登录接口和验证码接口
                .excludePathPatterns("/admin/system/sysUser/login" ,
                        "/admin/system/sysUser/generateValidateCode"
                )
                //接口白名单:swagger
                .excludePathPatterns("/swagger-resources/**",
                        "/webjars/**", "/v2/**",
                        "/swagger-ui.html/**")
                .addPathPatterns("/**");
    }

    @Override
    public void addCorsMappings(CorsRegistry registry) {
            registry.addMapping("/**")
                        .allowedOrigins("*")
                    .allowedMethods("*")
                    .allowedHeaders("*")
                    .allowCredentials(true);

    }
}
